package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 根据客户查询账户
 * 
 * @author 甄玉晴 2020年4月7日
 */
public class QueryAccountByCtm implements ServerInterface {

	@SuppressWarnings("rawtypes")
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			// 入参转换
			CtmInfoBean bean = JsonUtils.objectToPojo(req.getData(), CtmInfoBean.class);
			if (StringUtils.isBlank(bean.getCtmNo())) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "客户编号不能为空。", Void.class);
			}
			AccountInfoMapper mapper = factory.getMapper(AccountInfoMapper.class);
			List<AccountInfoBean> list = new ArrayList<AccountInfoBean>();
			// 根据客户编号查询账户列表
			list = mapper.getAccountInfoByCtmNo(bean.getCtmNo(), AccountInfoBean.ACCOUNT);
			//获取数据字典信息
			for(AccountInfoBean accountInfo:list) {
				if (StringUtils.isNotBlank(accountInfo.getAccountType())) {
					accountInfo.setAccountType(Tools.getDicNameByCodeAndValue(factory, "ACT", accountInfo.getAccountType()));
				}
				if (StringUtils.isNotBlank(accountInfo.getPayWay())) {
					accountInfo.setPayWay(Tools.getDicNameByCodeAndValue(factory, "BPW", accountInfo.getPayWay()));
				}
			}
			// 返回结果集
			MessageBean<List> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "sucess", List.class);
			msg.setData(list);
			return msg;
		} catch (Exception e) {
			logger.error("根据客户编号查询账户查询失败" + e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", Void.class);
		}
	}

}
